<?php

class test_bk
{
	function global_getImageAndDesigner(&$b)
	{
		if (array_key_exists('img_small_id',$b) == false)	return $b ;
	
		$b['image'] = array() ;
		util_set_IMAGE($b['image'] , $b) ;
		
		if (array_key_exists('designer_id',$b) == true && $b['designer_id'] > 0){
			$info = sql_fetch_array("select * from user_info_ex where `user_id` = '{$b['designer_id']}'", "u_key") ;
			if (is_array($info) && array_key_exists("img_small_id", $info)){				
				util_set_IMAGE3($b['image'] , $info['img_small_id']['u_value'] , $info['img_thumb_id']['u_value'] , $info['img_url_id']['u_value']) ;
				$de = sql_fetch_one("select * from user_info where `id` = '{$b['designer_id']}'") ;
				if (is_array($de) && array_key_exists("username", $de)){
					$b['designer']		= $de ;
					$b['designer']['name'] = $de['username']  ;
				}
			}
		}
		return $b ;
	}

	function get_table_for_page($offset , $limit , $lastpage , $filter_name , $tablename , $where = "")
	{
		$ret = array() ;
		$ret['data'] = array() ;
		
		if ($offset < 1)	$offset = 1 ;
		
		if ($filter_name != ""){
			if ($where == "")			$where = " where `name` like '%$filter_name%'" ;
			else 						$where .= " and `name` like '%$filter_name%'" ;
		}
		
		$orderby = "" ;
		if ($tablename == 'sys_item' || $tablename == 'show' || $tablename == 'taozhuang'){
			$orderby = " order by `top_time` desc " ;
		}
		
		// 组织页面数据
		$start = ($offset - 1) * $limit ;	
		$allcount = sql_fetch_one_cell("select count(*) from `$tablename` $where ") ;
		$ret['data']['pagination'] = array() ;
		util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
		
		if ($lastpage == true){
			$offset = $ret['data']['pagination']['count'] ;
			$start = ($offset - 1) * $limit ;	
			$ret['data']['pagination'] = array() ;
			util_set_PAGINATION($ret['data']['pagination'] , $allcount , $limit , $offset) ;
		}
				
		$bitem = sql_fetch_rows("select * from `$tablename` $where  $orderby  limit $start , $limit") ;	
		foreach ($bitem as &$b){
			$this->global_getImageAndDesigner($b) ;
		}	
		$ret['data']['item'] = $bitem ;
		
		if ($tablename == 'taozhuang'){
			foreach($ret['data']['item'] as &$oo){
				$oo['tc_b_id'] = intval($oo['state_tree']) ;

				$brand = $oo['tc_b_id'] ;
                $onebd = sql_fetch_one("select * from prf_tz_brand where id = '$brand'") ;
                if (is_array($onebd) && array_key_exists("name", $onebd)){
                    $oo['tc_b_name'] = $onebd['name'] ;
                }else{
                    $oo['tc_b_name'] = "" ;
                }
			}
			$ret['data']['bao'] = array() ;
            $onebds = sql_fetch_rows("select * from prf_tz_brand") ;

			for($i = 0 ; $i < count($onebds) ; ++ $i){
				$ret['data']['bao'][] = array("tc_b_id"=>$onebds[$i]['id'] , "tc_b_name"=>$onebds[$i]['name']) ;
			}
			
		}
		
		return $ret ;
	}	
	/**	 * 
	 * 获取某张特定图片id的图片信息
	 * @param unknown_type $itemid
	 */
	function get_image_for_id($imgid)
	{
		if ($imgid > 0){
			$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
			if (is_array($imgurl) && array_key_exists('url', $imgurl)){
				return array($imgid , $imgurl['url'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}else{
				return array(0,"",0,0,"") ;
			}
		}
		return array(0,"",0,0,"") ;
	}
	/**
	 * 获取单品的默认透视图片详细信息
	 * @param sys_item表的id字段
	 * 返回数组[图片id , url地址 , 宽度 , 高度  , 服务器存放绝对地址]
	 */
	function get_item_default_ts_image($itemid)
	{
		$onevalue = sql_fetch_one("select b.img_tz_id , i.`name` from sys_item i left join base_sys_preview b on i.id = b.item_id where i.id = '$itemid' and b.`img_tz_id` > 0 order by b.`id` asc limit 1") ;
		if (is_array($onevalue) && array_key_exists('img_tz_id', $onevalue)){
			$imgid = intval($onevalue['img_tz_id']) ;
			if ($imgid > 0){
				$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
				return array($imgid , $imgurl['url'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}
		}
		$onevalue = sql_fetch_one("select img_id from taozhuang_pos where item_id = $itemid limit 1") ;
		if (is_array($onevalue) && array_key_exists('img_id', $onevalue)){
			$imgid = intval($onevalue['img_id']) ;
			if ($imgid > 0){
				$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
				return array($imgid , $imgurl['url'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}
		}
		return array(0,"",0,0,"") ;
	}
	
	/**
	 * 获取套装某位置的图片详细信息
	 * @param taozhuang_pos表的id字段
	 * 返回数组[图片id , url地址 , 图片类型 , 图片名称 , 宽度 , 高度  , 服务器存放绝对地址]
	 */
	function get_taozhuang_pos_for_ts_image($posid)
	{
		$onevalue = sql_fetch_one("select t.img_id , b.img_tz_id , i.`name` from taozhuang_pos t inner join sys_item i on t.item_id = i.id left join base_sys_preview b on i.id = b.item_id and b.`img_tz_id` > 0 where t.id = '$posid'  order by b.`id` asc") ;
		if (is_array($onevalue) && array_key_exists('img_id', $onevalue)){
			$imgid = intval($onevalue['img_id']) ;
			if ($imgid > 0){
				$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
				return array($imgid , $imgurl['url'] , $imgurl['type'] , strlen($onevalue['name']) > 0 ? $onevalue['name'] : $imgurl['name'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}
			$imgid = intval($onevalue['img_tz_id']) ;
			if ($imgid > 0){
				$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
				
				return array($imgid , $imgurl['url'] , $imgurl['type'] , strlen($onevalue['name']) > 0 ? $onevalue['name'] : $imgurl['name'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}
		}
		return array(0,"",0,"",0,0,"") ;
	}
	/**
	 * 获取DIY套装某位置的图片详细信息
	 * @param diy_pos表的id字段 , $type=1 diy模板 , > 1 mydiy的posid
	 * 返回数组[图片id , url地址 , 图片类型 , 图片名称 , 宽度 , 高度  , 服务器存放绝对地址]
	 */
	function get_diy_pos_for_ts_image($posid , $type = 1)
	{
		if ($type == 1){
			$onevalue = sql_fetch_one("select t.img_id , b.img_tz_id , i.`name`,i.`class_id`,t.item_id from diy_pos t inner join sys_item i on t.item_id = i.id left join base_sys_preview b on i.id = b.item_id and b.`img_tz_id`>0  where t.id = '$posid' order by b.`id`") ;
		}else{
			$onevalue = sql_fetch_one("select t.img_id , b.img_tz_id , i.`name`,i.`class_id`,t.item_id from user_diy_pos t inner join sys_item i on t.item_id = i.id left join base_sys_preview b on i.id = b.item_id and b.`img_tz_id`>0  where t.id = '$posid' order by b.`id`") ;
		}
		if (is_array($onevalue) && array_key_exists('img_tz_id', $onevalue)){
			$imgid = intval($onevalue['img_id']) ;
			if ($imgid > 0){
				$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
				return array($imgid , $imgurl['url'] , $onevalue['class_id'] , strlen($onevalue['name']) > 0 ? $onevalue['name'] : $imgurl['name'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}
			$imgid = intval($onevalue['img_tz_id']) ;
			if ($imgid > 0){
				$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;				
				return array($imgid , $imgurl['url'] , $onevalue['class_id'] , strlen($onevalue['name']) > 0 ? $onevalue['name'] : $imgurl['name'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
			}
		}
		$itemid = $onevalue['item_id'] ;
		if (intval($itemid) > 0){
			$twovalue = sql_fetch_one("select img_id from taozhuang_pos where item_id = $itemid limit 1") ;
			if (is_array($twovalue) && array_key_exists('img_id', $twovalue)){
				$imgid = intval($twovalue['img_id']) ;
				if ($imgid > 0){
					$imgurl = sql_fetch_one("select CONCAT('{$GLOBALS['URL_IMG_HEAD']}' , `url`) as url , CONCAT('{$GLOBALS['upload_Path']}' , `url`) as path_url , `type` , `name` , `width` , `height` from sys_image where `id` = '$imgid'") ;
					return array($imgid , $imgurl['url'] , $onevalue['class_id'] , strlen($onevalue['name']) > 0 ? $onevalue['name'] : $imgurl['name'] , $imgurl['width'] , $imgurl['height'] , $imgurl['path_url'] ) ;
				}
			}
		}
		return array(0,"",0,"",0,0,"") ;
	}
}


function  test_util_set_IMAGE(&$arr , $img_id)
{
	$arr['img_url']   = "" ;
	$arr['img_type']  = 0 ;
	$arr['img_name']  = "" ;
	$arr['img_width']  = 0 ;
	$arr['img_height']  = 0 ;

	if ($img_id > 0){
		$addr = sql_fetch_one("select * from `sys_image` where `id` = '$img_id'") ;
		if (array_key_exists('url', $addr)){
			$arr['img_url']   = $GLOBALS['URL_IMG_HEAD'].$addr['url'] ;
			$arr['img_type']  = $addr['type'] ;
			$arr['img_name']  = $addr['name'] ;
			$arr['img_width']  = $addr['width'] ;
			$arr['img_height']  = $addr['height'] ;
		}
	}
}

function test_util_get_IMAGE_url($img_id)
{
	if ($img_id > 0){
		$addr = sql_fetch_one("select * from `sys_image` where `id` = '$img_id'") ;
		if (array_key_exists('url', $addr)){
			return $GLOBALS['URL_IMG_HEAD'].$addr['url'] ;
		}
	}
	return "" ;
}
function test_util_get_IMAGE_Addr($img_id)
{
	if ($img_id > 0){
		$addr = sql_fetch_one("select * from `sys_image` where `id` = '$img_id'") ;
		if (array_key_exists('url', $addr)){
			return $GLOBALS['upload_Path'].$addr['url'] ;
		}
	}
	return "" ;
}
?>